﻿using System.Threading.Tasks;

namespace LujWeb.Internal;

public class RequestHandleInvoker {

  public Task Invoke<M, C>(WebRequestHandler<M, C> handler, C ctx, string routePath)
      where M : WebHandleMeta0
      where C : WebHandleContext {

    // asp会把带和不带斜杠结尾的都混在一起回调，所以这里手动统一
    if (routePath.EndsWith('/')) {
      string reqPath = ctx.RequestPath;
      if (!reqPath.EndsWith('/')) {
        return ctx.ResponseRedirect(reqPath + "/");
      }
    }

    return handler.OnHandle(ctx);
  }
}
